package Renderer;


import utilities.Image;
import utilities.TextureDecoder;

import java.nio.ByteBuffer;

public class BackgroundImage {

    Image img;
    int max_dim;
    int dim;
    String filename;

    public BackgroundImage(String file, int max_dim){
        img = TextureDecoder.LoadImage(file);
        this.max_dim = max_dim;
        filename = file;
        dim = Math.min(max_dim, Math.min(img.width, img.height));
    }

    public boolean isLoaded(String file){
        return filename.compareTo(file) == 0;
    }
    public int getWParts(){
        return (int)Math.ceil(img.width/dim);
    }

    public int getHParts(){
        return (int)Math.ceil(img.height/dim);
    }
    public int getDim(){
        return dim;
    }

    public Image getImage(){
        return img;
    }

    public ByteBuffer getPart(int x_pos, int y_pos){
        ByteBuffer buf = ByteBuffer.allocateDirect(dim * dim);
        byte tmp[] = new byte[dim];
        for(int i = 0; i < dim; i++){
            img.data.get(tmp, ((x_pos * dim) + i) * img.width + (y_pos * dim) , dim);
            buf.put(tmp);
        }
        buf.flip();
        return buf;
    }
}

